package org.jeecg.modules.plan.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.plan.entity.PlanInfo;
import org.jeecg.modules.plan.entity.PlanInfoDetail;
import org.jeecg.modules.plan.mapper.PlanInfoDetailMapper;
import org.jeecg.modules.plan.mapper.PlanInfoMapper;
import org.jeecg.modules.plan.service.IPlanInfoService;
import org.jeecg.modules.system.model.SysUserSysDepartModel;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 计划表
 * @Author: jeecg-boot
 * @Date:   2021-08-14
 * @Version: V1.0
 */
@Service
public class PlanInfoServiceImpl extends ServiceImpl<PlanInfoMapper, PlanInfo> implements IPlanInfoService {

	@Autowired
	private PlanInfoMapper planInfoMapper;
	@Autowired
	private PlanInfoDetailMapper planInfoDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(PlanInfo planInfo, List<PlanInfoDetail> planInfoDetailList) {
		planInfoMapper.insert(planInfo);
		if(planInfoDetailList!=null && planInfoDetailList.size()>0) {
			for(PlanInfoDetail entity:planInfoDetailList) {
				//外键设置
				entity.setPlanInfoId(planInfo.getId());
				planInfoDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(PlanInfo planInfo,List<PlanInfoDetail> planInfoDetailList) {
		planInfoMapper.updateById(planInfo);
		
		//1.先删除子表数据
		planInfoDetailMapper.deleteByMainId(planInfo.getId());
		
		//2.子表数据重新插入
		if(planInfoDetailList!=null && planInfoDetailList.size()>0) {
			for(PlanInfoDetail entity:planInfoDetailList) {
				//外键设置
				entity.setPlanInfoId(planInfo.getId());
				planInfoDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		planInfoDetailMapper.deleteByMainId(id);
		planInfoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			planInfoDetailMapper.deleteByMainId(id.toString());
			planInfoMapper.deleteById(id);
		}
	}

	@Override
	public IPage<PlanInfo> auditList(String orgCode, String realname, String planTile, IPage page) {
		List<PlanInfo> list = planInfoMapper.auditList(page, orgCode, realname,planTile);
		page.setRecords(list);

		return page;
	}
}
